Random numbers in standard C++


Random numbers in standard C++

The statistic properties of the numbers generated by the function rand() can be improved by using the CPP standard instead. This library includes several options to adjust the distribution of the random generator.
Las propiedades estadísticas de los números generados por la función rand() pueden ser mejoradas usando la librería CPP estándar en su lugar. Esta librería incluye varias opciones para ajustar la distribución del generador aleatorio.

Problem 1
Create a program called Play to generate 10 integer random values from 0 to 9 using a probabilistic uniform distribution. You need to insert a textbox called tbxOutput for this program. Do not forget to set the properties of the textbox: multi-line and read-only.
Cree un programa llamado Play para generar 10 valores aleatorios enteros desde 0 a 9 usando una distribución de probabilidad uniforme. Usted necesita insertar una caja de texto llamada tbxOutput para este programa. No se olvide de fijar las propiedades de multi-line y read-only en la caja de texto.

Play

Play.h
#pragma once //______________________________________ Play.h
#include "resource.h"
class Play: public Win::Dialog
{
public:
     Play()
     {
          random_device rd;
          randomGenerator.seed(rd());
     }
     ~Play()
     {
     }
     // ___________________________________________ Linear congruential generator
     //std::minstd_rand randomGenerator;
     //___________________________________________ Mersenne twister engine
     std::mt19937 randomGenerator;
protected:
     . . .
};


Play.cpp
. . .
void Play::Window_Open(Win::Event& e)
{
     //___________________________________________ Uniform integer distribution
     wchar_t text[32];
     std::uniform_int_distribution<int> distribution(0, 9); // [0 9]
     int x = 0;
     for(int i = 0; i < 10; ++i)
     {
          x = distribution(randomGenerator);
          _snwprintf_s(text, 32, _TRUNCATE, L"%d\r\n", x);
          tbxOutput.Text += text;
     }
}


Tip
The libraries to generate random numbers are included in the file called random that it is automatically included by all Wintempla projects.
Las librerías para números aleatorios están incluidas en el archivo llamado random que es automáticamente incluido en todos los proyectos de Wintempla.

Problem 2
Create a program called PlayDouble to generate 2000 floating point random using a normal distribution.
  1. Create the project
  2. Open Wintempla
  3. In the toolbar click theShow All Controls in ToolboxShow All Controls in Toolbox
  4. Draw a histogram control in the GUI
  5. Complete the GUI as shown below
  6. Double click the textbox for the mean. In the Events tab, check the Change event
  7. Double click the textbox for the variance. In the Events tab, check the Change event

Cree un programa llamado PlayDouble para generar 2000 números de punto flotante usando una distribución de probabilidad normal.
  1. Cree el proyecto
  2. Abra Wintempla
  3. En la barra de herramientas haga clic enShow All Controls in ToolboxShow All Controls in Toolbox
  4. Dibuje un control de Histogram en la GUI
  5. Complete la GUI como se muestra debajo
  6. Haga clic en la caja de texto para el media (mean). En la pestaña de Events, marque el evento Change
  7. Haga clic en la caja de texto para la varianza (variance). En la pestaña de Events, marque el evento Change

PlayDoubleGui

PlayDouble1

PlayDouble2

PlayDouble.h
#pragma once //______________________________________ PlayDouble.h
#include "resource.h"
class PlayDouble: public Win::Dialog
{
public:
     PlayDouble()
     {
          randomGenerator.seed(::GetTickCount());
     }
     ~PlayDouble()
     {
     }
     std::mt19937 randomGenerator;
     void RefreshHistogram();
protected:
     . . .
};


PlayDouble.cpp
. . .
void PlayDouble::Window_Open(Win::Event& e)
{
     this->tbxMean.DoubleValue = 40.0;
     this->tbxVariance.DoubleValue = 5.0;
     RefreshHistogram();
}

void PlayDouble::tbxVariance_Change(Win::Event& e)
{
     RefreshHistogram();
}

void PlayDouble::tbxMean_Change(Win::Event& e)
{
     RefreshHistogram();
}

void PlayDouble::RefreshHistogram()
{
     valarray<double> data(2000);
     const double mean = tbxMean.DoubleValue;
     const double variance = tbxVariance.DoubleValue;
     if (variance == 0.0) return;
     std::normal_distribution<double> distribution(mean, variance);
     for(int i = 0; i<2000; i++)
     {
          data[i] = distribution(randomGenerator);
     }
     histOutput.MinX = mean-20.0*sqrt(variance);
     histOutput.MaxX = mean+20.0*sqrt(variance);
     const double maxFreq = histOutput.SetData(data, 40, false);
     histOutput.MinY = 0.0;
     histOutput.MaxY = maxFreq;
     histOutput.CaptionY = L"Frequency";
}


Tip
The table below presents briefly some of the probability distributions supported.
La tabla de abajo presenta en forma breve algunas de las distribuciones de probabilidad incluidas.

Name    Example  
Integer Uniform std::uniform_int_distribution distribution(1, 10);
Double Uniform std::uniform_real_distribution distribution (3.0, 7.0);
Normal std::normal_distribution distribution (3.0, 7.0);
Bernoulli std::bernoulli_distribution distribution (0.22);
Binomial std:: binomial_distribution distribution(1.0/6.0);
Exponential std::exponential_distribution
Gamma std::gamma_distribution distribution(3.0);
Geometric std::geometric_distribution distribution(0.1);
Poisson std::poisson_distribution distribution(6.0);

© Copyright 2000-2026 Wintempla selo. All Rights Reserved. abr. 17 2026. Home